package com.ssh.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.ssh.entity.Dept;
import com.ssh.entity.Emp;
import com.ssh.entity.PageBean;
import com.ssh.service.DeptService;
import com.ssh.service.EmpService;

public class EmpAction extends ActionSupport implements ModelDriven<Emp>{
	private static final long serialVersionUID = 1L;
	//模型驱动
	private Emp emp=new Emp();
	@Override
	public Emp getModel() {
		return emp;
	}
	
	//注入empService
	private EmpService empService;
	public void setEmpService(EmpService empService) {
		this.empService = empService;
	}
	//注入DeptService
	private DeptService deptService;
	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}
	//跳转到添加页面
	public String addPage(){
		//查询部门信息
		List<Dept> list=deptService.findAll();
		ServletActionContext.getRequest().setAttribute("list", list);
		return "addPage";
	}
	//添加
	public String add(){
		empService.add(emp);
		return "add";
	}
	//查询
	public String findAll(){
		List<Emp> list=empService.findAll();
		ServletActionContext.getRequest().setAttribute("list", list);
		return "findAll";
	}
	
	/**
	 * 根据用户名查询
	 */
	public String findByName(){
		Emp list = empService.fingByName(emp.getEusername());
		if(list !=null){
			return "findAll";
		}
		return null;
	}
	
	//删除
	public String delete(){
		//获取id
		int eid=emp.getEid();
		Emp emp=empService.findById(eid);
		//删除
		empService.delete(emp);
		return "delete";
	}
	//跳转到修改页面
	public String updatePage(){
		//查询员工信息
		int eid=emp.getEid();
		Emp emp=empService.findById(eid);
		//查询部门信息
		List<Dept> list=deptService.findAll();
		ServletActionContext.getRequest().setAttribute("emp", emp);
		ServletActionContext.getRequest().setAttribute("list", list);
		return "updatePage";
	}
	//修改
	public String update(){
		
		empService.update(emp);
		return "update";
	}
	/**
	 * 封装当前页
	 * @return
	 */
	private Integer currenPage;
	public Integer getCurrenPage() {
		return currenPage;
	}
	public void setCurrenPage(Integer currenPage) {
		this.currenPage = currenPage;
	}
	
	public String listPage(){
		PageBean pageBean=empService.listPage(currenPage);
		ServletActionContext.getRequest().setAttribute("pageBean", pageBean);
		//ActionContext.getContext().getValueStack().push(pageBean);
		//ActionContext.getContext().put("pageBean", pageBean);
		return "listPage";
	}
}
